from django.http import JsonResponse
from django.forms.models import model_to_dict
import datetime
from django.db.models import Q
from sqlalchemy.util import reduce
import operator
from collection_manage.models import UserCollection
import base64
import json
import ssl
from adlist.models import ToutiaoModel, SinaModel, SouhuModel, FenghuangModel
import urllib
import random
from  ad_manage.models import Advertisement
from users.models import UserProfile
import os
def get_data_controller(page, pageSize, token, advertisement, public_time, discover_time, search_text, app_from):
    if token == "" or token == "null":
        all_count = advertisement.count()
        advertisement = advertisement.order_by('-create_time')[0:10]
        advertisements = {'json_data': [model_to_dict(item) for item in advertisement]}
        for i in advertisements.get("json_data"):
            if 'webp' in i['img']:
                i['img'] = i['img'].replace('webp', 'jpg')
        advertisements['all_count'] = all_count
        advertisements['show_count'] = 10
        return advertisements
    else:
        # 判断是否有“发布时间”的搜索条件
        if (public_time != '' and public_time != ' '):
            public_start = public_time.split('~')[0]
            public_end = public_time.split('~')[1]
            advertisement = advertisement.filter(public_time__range=(public_start, public_end))
            if (advertisement.count() == 0):
                return {'json_data': [model_to_dict(item) for item in advertisement]}

        # 判断是否有“发现时间”的搜索条件
        if (discover_time != '' and discover_time != ' '):
            discover_start = discover_time.split('~')[0]
            discover_start = datetime.datetime.strptime(discover_start, "%Y-%m-%d")
            discover_end = discover_time.split('~')[1]
            discover_end = datetime.datetime.strptime(discover_end, "%Y-%m-%d")
            advertisement = advertisement.filter(create_time__range=(discover_start, discover_end))
            if (advertisement.count() == 0):
                return {'json_data': [model_to_dict(item) for item in advertisement]}

        # 判断是否有搜索广告title的搜索条件
        if (None != search_text and '' != search_text and ' ' != search_text):
            advertisement = advertisement.filter(Q(title__contains=search_text)).order_by('-create_time')[
                            (page - 1) * pageSize:page * pageSize]
            advertisements = {'json_data': [model_to_dict(item) for item in advertisement]}
            for i in advertisements.get("json_data"):
                # 将webp结尾的外部图片链接换成jpg结尾，解决火狐浏览器打不开问题
                if 'webp' in i['img']:
                    i['img'] = i['img'].replace('webp', 'jpg')
            all_count = advertisement.count()
            advertisements['all_count'] = all_count
            advertisements['show_count'] = all_count // 3
            return advertisements

        # 判断是否有搜索“平台”的搜索条件
        if ('' != app_from):
            app_from_arr = app_from[0:len(app_from) - 1].replace(' ', '').split(',')
            advertisement = advertisement.filter(
                reduce(operator.or_, map(lambda x: Q(app_name__contains=x), app_from_arr)))

        # 统计搜索结果数量
        all_count = advertisement.count()
        advertisement = advertisement.order_by('-create_time')[(page - 1) * pageSize:page * pageSize]
        user_id = getIdByToken(token)

        advertisements = {'json_data': [model_to_dict(item) for item in advertisement]}
        is_vip_ad = UserProfile.objects.filter(id= user_id)

        advertisements['all_count'] = all_count
        advertisements['show_count'] = all_count // 3
        asd = advertisements.get("json_data")
        for i in advertisements.get("json_data"):
            advertisement_id = int(i.get('id'))
            # 将webp结尾的外部图片链接换成jpg结尾，解决火狐浏览器打不开问题
            if 'webp' in i['img']:
                i['img'] = i['img'].replace('webp', 'jpg')
            # 判断当前广告是否已收藏
            if (len(UserCollection.objects.filter(advertisement_id=advertisement_id).filter(user_id=user_id)) > 0):
                i['is_collect'] = 1





        return advertisements


def getIdByToken(token):
    payload = token.split(".")[1] + "=="
    a = base64.b64decode(payload).decode('unicode_escape')
    user_info = json.loads(a)
    return user_info.get("user_id")


def toutiao_object(toutiao, advertisement):
    advertisement.url = toutiao.article_url
    if (toutiao.article_url == None or toutiao.article_url == '' or toutiao.article_url == ' '):
        return None
    if (
            toutiao.large_image_list == None or toutiao.large_image_list == '' or toutiao.large_image_list == ' '):
        return None
    advertisement.title = toutiao.title
    img = toutiao.large_image_list[1:len(toutiao.large_image_list) - 1].replace('\'', '"')

    img_list = img.split(', {"height"')
    if img_list[0][0:1] != '{':
        img_list[0] = '{' + img_list[0] + '}'
    img = json.loads(img_list[0]).get('url')
    advertisement.img = img
    if (img == '' or img == ' '):
        img = json.loads(
            toutiao.large_image_list[1:len(toutiao.large_image_list) - 1].replace('\'', '"')).get(
            'uri')
    img_exist = "http://imagethinking.semyes.cn//images/7/" + toutiao.local_img
    r = requests.get(img_exist)
    if r.status_code != requests.codes.ok:
        return None
    advertisement.img = img
    advertisement.read_count = toutiao.read_count
    advertisement.share_count = toutiao.share_count
    advertisement.source = toutiao.source
    advertisement.create_time = toutiao.create_time
    advertisement.update_time = toutiao.update_time

    advertisement.app_id = 7
    advertisement.app_from = "今日头条"
    advertisement.logo = '../../../static/images/app_logo/toutiao.png'
    if toutiao.amount == None:
        advertisement.discover_count = 0
    advertisement.discover_count = toutiao.amount
    advertisement.random_id = random.randint(0, 30000)
    advertisement.model_from = 'toutiao_app'
    advertisement.app_name = '今日头条'
    if toutiao.local_img != None:
        advertisement.local_img = toutiao.local_img

    return advertisement


def fenghuang_object(fenghuang, advertisement):
    img_exist = "http://imagethinking.semyes.cn//images/3/" + fenghuang.local_img
    r = requests.get(img_exist)
    if r.status_code != requests.codes.ok:
        return None
    advertisement.url = fenghuang.weburl
    advertisement.title = fenghuang.title
    advertisement.img = fenghuang.thumbnail
    advertisement.source = fenghuang.source
    advertisement.create_time = fenghuang.create_time
    advertisement.update_time = fenghuang.update_time
    advertisement.discover_count = fenghuang.amount
    advertisement.app_from = "凤凰新闻"
    advertisement.logo = '../../../static/images/app_logo/fenghuang.png'
    advertisement.random_id = random.randint(0, 30000)
    advertisement.model_from = 'fenghuang_app'
    advertisement.app_name = '凤凰新闻'
    advertisement.app_id = 3
    if fenghuang.local_img != None:
        advertisement.local_img = fenghuang.local_img
    localimg = fenghuang.local_img.replace('[', '').replace(']', '').replace('\'', '').replace('\\', '')

    return advertisement


def sina_object(sina, advertisement):

    advertisement.url = sina.link
    advertisement.title = sina.title
    advertisement.img = sina.pic
    advertisement.create_time = sina.create_time
    advertisement.update_time = sina.update_time
    advertisement.app_from = "新浪新闻"
    advertisement.discover_count = sina.amount
    advertisement.logo = '../../../static/images/app_logo/sina.png'
    advertisement.random_id = random.randint(0, 30000)
    advertisement.model_from = 'sina_app'
    advertisement.app_name = '新浪新闻'
    advertisement.app_id = 5
    if sina.local_img != None:
        advertisement.local_img = sina.local_img
    else:
        return None
    img_exist = "http://imagethinking.semyes.cn//images/5/" + sina.local_img
    r = requests.get(img_exist)
    if r.status_code != requests.codes.ok:
        return None
    return advertisement

import requests
def souhu_object(souhu, advertisement):

    resource = souhu.resource1.replace('\'', '"').replace(' ', '')

    if resource == ' ' or resource == '':
        return None
    # print(resource)
    if resource[0:1] != '{':
        resource = resource[1:-1]
    j = json.loads(resource)
    img = j.get('file')
    if (img == '' or img == None):
        return None
    if souhu.local_img ==None:
        return None
    localimg = souhu.local_img.replace('[','').replace(']','').replace('\'','').replace('\\','')
    img_exist = "http://imagethinking.semyes.cn//images/6/" + localimg
    r = requests.get(img_exist)
    if r.status_code != requests.codes.ok:
        return None

    advertisement.url = souhu.link
    advertisement.title = souhu.resource_text
    advertisement.img = img
    advertisement.create_time = souhu.create_time
    advertisement.update_time = souhu.update_time
    advertisement.app_from = "搜狐新闻"
    advertisement.discover_count = souhu.amount
    advertisement.logo = '../../../static/images/app_logo/souhu.png'
    advertisement.random_id = random.randint(0, 30000)
    advertisement.model_from = 'souhu_app'
    advertisement.app_name = '搜狐新闻'
    advertisement.app_id = 6
    if souhu.local_img != None:
        img_list = souhu.local_img.split(',')
        advertisement.local_img = img_list[0].replace('[', '').replace(']', '').replace(',', '').replace('\'', '')
    return advertisement


def feedsradar_object(feedsradar, advertisement):
    img_exist = "/repo/smspider/images/feedsradar_app/" + feedsradar.local_img
    if not os.path.exists(img_exist):
        return None
    if feedsradar.un_watermark == '' or feedsradar.un_watermark == None:
        return None
    advertisement.url = feedsradar.url
    advertisement.title = feedsradar.title
    advertisement.img = feedsradar.img
    advertisement.source = feedsradar.advertiser
    advertisement.publish_time = feedsradar.publish_time
    advertisement.industry = feedsradar.industry
    advertisement.app_from = feedsradar.source
    advertisement.put_goal = feedsradar.target
    advertisement.create_time = feedsradar.create_time
    advertisement.update_time = feedsradar.update_time
    advertisement.discover_count = feedsradar.discover_count
    advertisement.hot = feedsradar.avg_index
    advertisement.app_id = 2
    advertisement.model_from = 'feedsradar_app'
    advertisement.random_id = random.randint(0, 30000)
    advertisement.logo = '../../../static/images/app_logo/' + feedsradar.source + '.jpg'

    if '百度' in feedsradar.source:
        advertisement.app_name = '百度信息流'
    elif '网易' in feedsradar.source:
        advertisement.app_name = '网易新闻'
    elif '新浪' in feedsradar.source:
        advertisement.app_name = '新浪新闻'
    elif '腾讯' in feedsradar.source or '手腾' in feedsradar.source:
        advertisement.app_name = '腾讯新闻'
    else:
        advertisement.app_name = feedsradar.source
    if (feedsradar.un_watermark != None):
        advertisement.local_img = feedsradar.un_watermark

    return advertisement


def toutiaodownload(request):
    image_path = '/repo/smspider/images/toutiao_app/'
    toutiaoModels = ToutiaoModel.objects.all()
    print(toutiaoModels.count())
    error = 0
    for toutiao in toutiaoModels:
        filename = ""
        save_local_img = ""
        if (toutiao.large_image_list == None or toutiao.large_image_list == '' or toutiao.large_image_list == ' '):
            error = error + 1
            continue
        img = toutiao.large_image_list[1:len(toutiao.large_image_list) - 1].replace('\'', '"')
        img = json.loads(img).get('url')
        if (img == '' or img == ' '):
            img = json.loads(
                toutiao.large_image_list[1:len(toutiao.large_image_list) - 1].replace('\'', '"')).get('uri')
        if img is not None and img != '':
            try:
                if img.find('@') != -1:
                    img = str(str(img).split('@')[0])
                if ".webp" in img:
                    img = img.replace(".webp", ".jpg")

                if ".mp4" not in img:
                    img_file = str(img.split('/')[-1])
                    if img_file.find(".png") == -1 and img_file.find(".jpg") == -1 and img_file.find(".gif") == -1:
                        filename = img_file + '.jpg'
                    else:
                        if img_file.find("?") == -1:
                            filename = img_file
                        else:
                            filename = img_file.split('?')[0]

                    ssl._create_default_https_context = ssl._create_unverified_context
                    local_img = image_path + '%s' % str(filename)
                    urllib.request.urlretrieve(img, local_img)
                    save_local_img = filename
            except Exception as e:
                error = error + 1
                print("toutiaodownload Exception-->")
                nowTime = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')  # 现在
                print(nowTime)
                print(e)
        toutiao.local_img = save_local_img
        toutiao.save()
    print('头条错误：' + str(error))

    return JsonResponse({'json_data': 'success'})


def souhudownload(request):
    image_path = '/repo/smspider/images/souhu_app/'
    souhuModels = SouhuModel.objects.all()
    for souhu in souhuModels:
        error_count = 0
        imgs = []
        filename = []
        resource = souhu.resource1.replace('\'', '"')
        if resource == ' ' or resource == '':
            error_count = error_count + 1
            continue
        j = json.loads(resource)
        img1 = j.get('file')
        if (img1 == '' or img1 == None):
            error_count = error_count + 1
            continue
        imgs.append(img1)

        img2 = ''
        img3 = ''
        # try:
        #     if souhu.resource2 != '' or souhu.resource2 is not None:
        #         if str(souhu.resource2).find('file') >= 0:
        #             img2 = json.loads(souhu.resource2.replace('\'', '"')).get('file')
        #             imgs.append(img2)
        #
        #     if souhu.resource3 != '' or souhu.resource3 is not None:
        #         if str(souhu.resource3).find('file') >= 0:
        #             img3 = json.loads(souhu.resource3.replace('\'', '"')).get('file')
        #             imgs.append(img3)
        # except Exception as e:
        #     print(e)

        img_num = 1
        if len(imgs):
            try:
                for img in imgs:
                    if ".webp" in img:
                        img = img.replace(".webp", ".jpg")

                    if ".mp4" not in img:
                        ssl._create_default_https_context = ssl._create_unverified_context
                        local_img = image_path + '%s' % str(img.split('/')[-1])
                        urllib.request.urlretrieve(img, local_img)
                        img_num += 1
                        filename.append(img.split('/')[-1])
                    else:
                        error_count = error_count + 1
                souhu.local_img = str(filename)
                souhu.save()
            except Exception as e:
                error_count = error_count + 1
                print(e)
        else:
            error_count = error_count + 1
    print(error_count)
    return JsonResponse({'json_data': 'success'})


def sinadownload(request):
    image_path = '/repo/smspider/images/sina_app/'
    sinaModels = SinaModel.objects.all()
    error = 0
    succ_count = 0
    for sina in sinaModels:
        if (sina.pic == '' or sina.pic == ' '):
            error = error + 1
            continue
        img = sina.pic
        filename = ""
        if img is not None and img != '':
            try:
                if ".webp" in img:
                    img = img.replace(".webp", ".jpg")

                if ".mp4" not in img:
                    ssl._create_default_https_context = ssl._create_unverified_context
                    local_img = image_path + '%s' % str(img.split('/')[-1])
                    urllib.request.urlretrieve(img, local_img)
                    filename = img.split('/')[-1]
                    succ_count = succ_count + 1
            except Exception as e:
                print("sinadownload Exception-->")
                nowTime = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')  # 现在
                print(nowTime)
                error = error + 1
                print(e)
        sina.local_img = str(filename)
        sina.save()
    print('共下载：' + str(succ_count))
    print('新浪错误：' + str(error))
    return JsonResponse({'json_data': 'success'})


def fenghuangdownload(request):
    image_path = '/repo/smspider/images/fenghuang_app/'
    fenghuangModels = FenghuangModel.objects.all()
    for fenghuang in fenghuangModels:
        filename = ""
        save_local_img = ""
        img = fenghuang.thumbnail
        error = 0
        if img is not None and img != '':
            try:
                if ".webp" in img:
                    img = img.replace(".webp", ".jpg")

                if ".mp4" not in img:
                    img_file = str(img.split('/')[-1])
                    if img_file.find(".png") == -1 and img_file.find(".jpg") == -1 and img_file.find(".gif") == -1:
                        filename = img_file + '.jpg'
                    else:
                        if img_file.find("?") == -1:
                            filename = img_file
                        else:
                            filename = img_file.split('?')[0]

                    ssl._create_default_https_context = ssl._create_unverified_context
                    local_img = image_path + '%s' % str(filename)
                    urllib.request.urlretrieve(img, local_img)
                    save_local_img = filename
            except Exception as e:
                error = error + 1
                print(e)
        fenghuang.local_img = save_local_img
        fenghuang.save()
    print('凤凰错误：' + str(error))
    return JsonResponse({'json_data': 'success'})


def radar_object(radar, advertisement):
    img_exist = "/repo/smspider/images/radar_app/" + radar.local_img
    if not os.path.exists(img_exist):
        return None
    if (radar.url == '' or radar.url == ' ' or radar.url == None):
        return None
    advertisement.url = radar.url
    advertisement.title = radar.title
    advertisement.img = radar.img
    advertisement.industry = radar.industry
    advertisement.discover_count = radar.hot
    advertisement.create_time = radar.create_time
    advertisement.update_time = radar.update_time
    advertisement.app_from = radar.source
    advertisement.logo = radar.logo
    advertisement.random_id = random.randint(0, 30000)
    advertisement.model_from = 'radar_app'
    if '百度' in radar.source:
        advertisement.app_name = '百度信息流'
    elif '网易' in radar.source:
        advertisement.app_name = '网易新闻'
    elif '新浪' in radar.source:
        advertisement.app_name = '新浪新闻'
    elif '腾讯' in radar.source:
        advertisement.app_name = '腾讯新闻'
    else:
        advertisement.app_name = radar.source
    if radar.local_img != None:
        advertisement.local_img = radar.local_img
    advertisement.app_id = 4

    return advertisement
